package com.jse;

import java.io.IOException;
import java.util.regex.Pattern;

import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

public class Html {

	private static final Pattern IMG_PATTERN = Pattern.compile(
		      "[<]\\s*[iI][mM][gG]\\s*[^>]*[sS][rR][cC]\\s*[=]\\s*['\"]([^'\"]*)['\"][^>]*[>]");
	private static final Pattern DIV_PATTERN = Pattern.compile(
		      "<p(?:(?!<\\/p>).|\\n)*?<\\/p>");
	private static final Pattern a_PATTERN = Pattern.compile(
		      "<a(?:(?!<\\/a>).|\\n)*?<\\/a>");
	
	public static void toImg(String bin,String url,String path) {
		//网页cmd|sh执行 wkhtmltoimage www.iepsy.com bing.png
		//wkhtmltoimage file:///D:/work/xjy/web/index3.html bing.png
		try {
			var p=Runtime.getRuntime().exec(new String[] {bin,url,path});
			var in=p.getInputStream();
			System.out.println(Io.readString(in));
			p.destroy();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static void toPdf(String bin,String url,String path) {
		//网页cmd|sh执行 wkhtmltopdf www.iepsy.com bing.png
		//wkhtmltopdf file:///D:/work/xjy/web/index3.html bing.png
		try {
			var p=Runtime.getRuntime().exec(new String[] {bin,url,path});
			var in=p.getInputStream();
			System.out.println(Io.readString(in));
			p.destroy();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, ClassCastException {
		String html=Fs.readString("D:\\work\\xjy\\web\\login.html");
//		var m=a_PATTERN.matcher(html);
//		while (m.find()) {
//			System.out.println(m.group());
//		}
		toImg("D:/wkhtmltox/bin/wkhtmltoimage","www.iepsy.com","d:/1.png");
//		toPdf("D:/wkhtmltox/bin/wkhtmltopdf","www.iepsy.com","d:/1.pdf");
	}
	
	public static Document doc(String h) throws ClassNotFoundException, InstantiationException, IllegalAccessException, ClassCastException {
		var x=org.w3c.dom.bootstrap.DOMImplementationRegistry.newInstance().getDOMImplementationList("");
		DOMImplementation a=(x.item(0));
		var d=a.createDocument(null,null, null);
		d.setNodeValue("html");
		d.createElement("html");
		Node nod;
		
		System.out.println(d);
		return null;
	}
}
